#!/bin/env python

# Merise python parser and models generators.
#
# @autor: Patrice Ferlet <metal3d@gmail.com>
# @version 0.0.2
# @licence Gnu/GPL v3

# $Id: merisier 49 2010-08-01 13:43:24Z Metal3d $
# $HeadURL: http://merisier.googlecode.com/svn/tags/0.0.2-beta1/src/merisier $

from merise.parser import *
from merise.graph import *
from merise.sql import *
import sys
from optparse import OptionParser

try:
    usage = "usage: %prog [options] merise-file"
    parser = OptionParser(usage)
    parser.add_option("-o", "--outdir", dest="directory", help="output directory", metavar="DIR", default="./")
    parser.add_option("-i", "--image-format", dest="imageformat", help="output image format (png, jpg, eps, svg, svgz... (default %default)", metavar="FORMAT", default="png")
    parser.add_option("-f", "--fontname", dest="fontname", help="fontname to use on nodes and arrows... default %default)", metavar="FONT", default="Sans")
    
    parser.add_option("-T", "--title-bg-color", dest="color", help="set title bg color, hexadecimal format (default to %default)", default="#ae7d4e")
    parser.add_option("-t", "--title-front-color", dest="fcolor", help="set title front color, hexadecimal format (default to %default)", default="#FFFFFF")
    parser.add_option("-b", "--background-color", dest="bgcolor", help="set diagram color, tranparent or hexadecimal format (default to %default)", default="white")
    parser.add_option("-d", "--fields-front-color", dest="frontcolor", help="set fields front color, hexadecimal format (default to %default)", default="#000000")
    parser.add_option("-e", "--fields-background-color", dest="fbgcolor", help="set fields background color, hexadecimal format (default to %default)", default="#FFFFFF")
    
    
    
    parser.add_option("-p", "--id-position", dest="idpos", metavar="POSITION", help="id position for keys (left or right, default %default)", default="right")
    parser.add_option("-s", "--sql-backend", dest="sqlbackend", metavar="BACKEND", help="Sql backend (can be mysql, sqlite, postgre or all that create every sql files - default is %default)", default="mysql")
    parser.add_option("-a", "--algo", dest="algo", metavar="ALGO", help="Placement algorythm for physical model, should be divide, modulo, random, even, odd, none (default is %default)", default="modulo")
    
    parser.add_option("-P", "--only-physical", dest="mpd", action="store_true", default=False, help="Generate only Physical Model image")
    parser.add_option("-C", "--only-conceptual", dest="mcd", action="store_true", default=False, help="Generate only Conceptual Model image")
    parser.add_option("-S", "--only-sql", dest="sql", action="store_true", default=False, help="Generate only SQL file")
    
    parser.add_option("-D", "--drop-shadow", dest="dropshadow", action="store_true", default=False, help="Add shadow behind tables")
    parser.add_option("-c", "--shadow", dest="shadow", help="Setup for shadow, use BLURxShadowSize+xoffset+yoffset (default to %default)", default="60x8+5+5")
    
    
    (options, args) = parser.parse_args()
    
    
    #check "only" options outputs
    MPD = True
    MCD = True
    SQL = True
    
    #switch off every options at first if one of "only" option is on
    if options.mpd or options.mcd or options.sql:
        MPD = False
        MCD = False
        SQL = False
    
    #then we activate outputs
    if options.mpd:
        MPD = True
        
    if options.mcd:
        MCD = True
        
    if options.sql:
        SQL = True
    
    
    try:
        options.filename = args[0]
    except:
        print "input file required"
        sys.exit(2)
except:
    sys.exit(1)

if not os.path.exists(options.directory):
    os.mkdir(options.directory)

if not os.path.isdir(options.directory):
	print options.directory+" is not a directory"
	sys.exit(3)


p = Parser(options)
p.parse()
    
g=Graph(p)
if MCD:
    g.generateMCD()
if MPD:    
    g.generateMPD()

if SQL:
    s=Sql(p)
    s.generate()
    
p.writeDataDict()
